#ifndef _SKTHREADS_HPP_
#define _SKTHREADS_HPP_

#include <pthread.h>
#include <iostream>

using namespace std;


// Allows multiple readers but only one writer can modify the record.
class SKThread
{
public:
    SKThread() : readerCount_(0), writer_(false), writerWaiting_(false) {}

    bool initialize();

    void readerLock();
    void writerLock();

    void readerUnlock();
    void writerUnlock();

private:

    pthread_mutex_t accessLock_, readLock_;
    pthread_cond_t accessCond_;

    // maintain reader and writer counts
    unsigned long readerCount_;
    bool writer_, writerWaiting_; // writerWaiting to avoid writer starvation
};


#endif //_SKTHREADS_HPP_
